home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / siscale.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  18.8 KB  |  633 lines

  1. /* Copyright (C) 1995, 2000 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: siscale.c,v 1.4 2000/09/19 19:00:50 lpd Exp $ */
  20. /* Image scaling filters */
  21. #include "math_.h"
  22. #include "memory_.h"
  23. #include "stdio_.h"
  24. #include <assert.h>
  25. #include "gconfigv.h"
  26. #include "gdebug.h"
  27. #include "strimpl.h"
  28. #include "siscale.h"
  29.  
  30. /*
  31.  *    Image scaling code is based on public domain code from
  32.  *      Graphics Gems III (pp. 414-424), Academic Press, 1992.
  33.  */
  34.  
  35. /* ---------------- ImageScaleEncode/Decode ---------------- */
  36.  
  37. /* Define whether to accumulate pixels in fixed or floating point. */
  38. #if USE_FPU <= 0
  39.  
  40.     /* Accumulate pixels in fixed point. */
  41.  
  42. typedef int PixelWeight;
  43.  
  44. #  if arch_ints_are_short
  45. typedef long AccumTmp;
  46. #  else
  47. typedef int AccumTmp;
  48. #  endif
  49.  
  50. /*
  51.  *    The optimal scaling for fixed point arithmetic is a function of the
  52.  *    size of AccumTmp type, the size if the input pixel, the size of the
  53.  *    intermediate pixel (PixelTmp) and the size of the output pixel.  This
  54.  *    is set by these definitions and the fraction_bits variables in the
  55.  *    functions.
  56.  */
  57. #define num_weight_bits\
  58.   ((sizeof(AccumTmp) - maxSizeofPixel) * 8 - (LOG2_MAX_ISCALE_SUPPORT + 1))
  59. #define numScaleBits  ((maxSizeofPixel - sizeof(PixelTmp)) * 8 )
  60. #define fixedScaleFactor  ((int) (1 << numScaleBits))
  61. #define scale_PixelWeight(factor) ((int)((factor) * (1 << num_weight_bits)))
  62. #define unscale_AccumTmp(atemp, fraction_bits) arith_rshift(atemp, fraction_bits)
  63. #define NEED_FRACTION_BITS
  64.  
  65. #else /* USE_FPU > 0 */
  66.  
  67.     /* Accumulate pixels in floating point. */
  68.  
  69. typedef float PixelWeight;
  70. typedef double AccumTmp;
  71.  
  72. #define num_weight_bits 0        /* Not used for floating point */
  73. #define fixedScaleFactor 1        /* Straight scaling for floating point */
  74. #define scale_PixelWeight(factor) (factor)
  75. #define unscale_AccumTmp(atemp, fraction_bits) ((int)(atemp))
  76. /*#undef NEED_FRACTION_BITS*/
  77.  
  78. #endif /* USE_FPU */
  79.  
  80. /* Temporary intermediate values */
  81. typedef byte PixelTmp;
  82. typedef int PixelTmp2;        /* extra width for clamping sum */
  83.  
  84. #define minPixelTmp 0
  85. #define maxPixelTmp 255
  86. #define unitPixelTmp 255
  87.  
  88. /* Max of all pixel sizes */
  89. #define maxSizeofPixel 2
  90.  
  91. /* Auxiliary structures. */
  92.  
  93. typedef struct {
  94.     PixelWeight weight;        /* float or scaled fraction */
  95. } CONTRIB;
  96.  
  97. typedef struct {
  98.     int index;            /* index of first element in list of */
  99.     /* contributors */
  100.     int n;            /* number of contributors */
  101.     /* (not multiplied by stride) */
  102.     int first_pixel;        /* offset of first value in source data */
  103. } CLIST;
  104.  
  105. /* ImageScaleEncode / ImageScaleDecode */
  106. typedef struct stream_IScale_state_s {
  107.     /* The client sets the params values before initialization. */
  108.     stream_image_scale_state_common;  /* = state_common + params */
  109.     /* The init procedure sets the following. */
  110.     int sizeofPixelIn;        /* bytes per input value, 1 or 2 */
  111.     int sizeofPixelOut;        /* bytes per output value, 1 or 2 */
  112.     double xscale, yscale;
  113.     void /*PixelIn */ *src;
  114.     void /*PixelOut */ *dst;
  115.     PixelTmp *tmp;
  116.     CLIST *contrib;
  117.     CONTRIB *items;
  118.     /* The following are updated dynamically. */
  119.     int src_y;
  120.     uint src_offset, src_size;
  121.     int dst_y;
  122.     uint dst_offset, dst_size;
  123.     CLIST dst_next_list;    /* for next output value */
  124.     int dst_last_index;        /* highest index used in list */
  125.     CONTRIB dst_items[MAX_ISCALE_SUPPORT];    /* ditto */
  126. } stream_IScale_state;
  127.  
  128. gs_private_st_ptrs5(st_IScale_state, stream_IScale_state,
  129.     "ImageScaleEncode/Decode state",
  130.     iscale_state_enum_ptrs, iscale_state_reloc_ptrs,
  131.     dst, src, tmp, contrib, items);
  132.  
  133. /* ------ Digital filter definition ------ */
  134.  
  135. /* Mitchell filter definition */
  136. #define Mitchell_support 2.0
  137. #define B (1.0 / 3.0)
  138. #define C (1.0 / 3.0)
  139. private double
  140. Mitchell_filter(double t)
  141. {
  142.     double t2 = t * t;
  143.  
  144.     if (t < 0)
  145.     t = -t;
  146.  
  147.     if (t < 1)
  148.     return
  149.         ((12 - 9 * B - 6 * C) * (t * t2) +
  150.          (-18 + 12 * B + 6 * C) * t2 +
  151.          (6 - 2 * B)) / 6;
  152.     else if (t < 2)
  153.     return
  154.         ((-1 * B - 6 * C) * (t * t2) +
  155.          (6 * B + 30 * C) * t2 +
  156.          (-12 * B - 48 * C) * t +
  157.          (8 * B + 24 * C)) / 6;
  158.     else
  159.     return 0;
  160. }
  161.  
  162. #define filter_support Mitchell_support
  163. #define filter_proc Mitchell_filter
  164. #define fproc(t) filter_proc(t)
  165. #define fWidthIn filter_support
  166.  
  167. /*
  168.  * The environment provides the following definitions:
  169.  *      typedef PixelTmp, PixelTmp2
  170.  *      double fproc(double t)
  171.  *      double fWidthIn
  172.  *      PixelTmp {min,max,unit}PixelTmp
  173.  */
  174. #define CLAMP(v, mn, mx)\
  175.   (v < mn ? mn : v > mx ? mx : v)
  176.  
  177. /* ------ Auxiliary procedures ------ */
  178.  
  179. /* Define the minimum scale. */
  180. #define min_scale ((fWidthIn * 2) / (MAX_ISCALE_SUPPORT - 1.01))
  181.  
  182. /* Calculate the support for a given scale. */
  183. /* The value is always in the range 1 .. MAX_ISCALE_SUPPORT. */
  184. private int
  185. contrib_pixels(double scale)
  186. {
  187.     return (int)(fWidthIn / (scale >= 1.0 ? 1.0 : max(scale, min_scale))
  188.          * 2 + 1);
  189. }
  190.  
  191. /* Pre-calculate filter contributions for a row or a column. */
  192. /* Return the highest input pixel index used. */
  193. private int
  194. calculate_contrib(
  195.     /* Return weight list parameters in contrib[0 .. size-1]. */
  196.              CLIST * contrib,
  197.     /* Store weights in items[0 .. contrib_pixels(scale)*size-1]. */
  198.     /* (Less space than this may actually be needed.) */
  199.              CONTRIB * items,
  200.     /* The output image is scaled by 'scale' relative to the input. */
  201.              double scale,
  202.     /* Start generating weights for input pixel 'input_index'. */
  203.              int input_index,
  204.     /* Generate 'size' weight lists. */
  205.              int size,
  206.     /* Limit pixel indices to 'limit', for clamping at the edges */
  207.     /* of the image. */
  208.              int limit,
  209.     /* Wrap pixel indices modulo 'modulus'. */
  210.              int modulus,
  211.     /* Successive pixel values are 'stride' distance apart -- */
  212.     /* normally, the number of color components. */
  213.              int stride,
  214.     /* The unit of output is 'rescale_factor' times the unit of input. */
  215.              double rescale_factor
  216. )
  217. {
  218.     double scaled_factor = scale_PixelWeight(rescale_factor);
  219.     double WidthIn, fscale;
  220.     bool squeeze;
  221.     int npixels;
  222.     int i, j;
  223.     int last_index = -1;
  224.  
  225.     if (scale < 1.0) {
  226.     double clamped_scale = max(scale, min_scale);
  227.  
  228.     WidthIn = fWidthIn / clamped_scale;
  229.     fscale = 1.0 / clamped_scale;
  230.     squeeze = true;
  231.     } else {
  232.     WidthIn = fWidthIn;
  233.     fscale = 1.0;
  234.     squeeze = false;
  235.     }
  236.     npixels = (int)(WidthIn * 2 + 1);
  237.  
  238.     for (i = 0; i < size; ++i) {
  239.     double center = (input_index + i) / scale;
  240.     int left = (int)ceil(center - WidthIn);
  241.     int right = (int)floor(center + WidthIn);
  242.  
  243.     /*
  244.      * In pathological cases, the limit may be much less
  245.      * than the support.  We do need to deal with this.
  246.      */
  247. #define clamp_pixel(j)\
  248.   (j < 0 ? (-j >= limit ? limit - 1 : -j) :\
  249.    j >= limit ? (j >> 1 >= limit ? 0 : (limit - j) + limit - 1) :\
  250.    j)
  251.     int lmin =
  252.     (left < 0 ? 0 : left);
  253.     int lmax =
  254.     (left < 0 ? (-left >= limit ? limit - 1 : -left) : left);
  255.     int rmin =
  256.     (right >= limit ?
  257.      (right >> 1 >= limit ? 0 : (limit - right) + limit - 1) :
  258.      right);
  259.     int rmax =
  260.     (right >= limit ? limit - 1 : right);
  261.     int first_pixel = min(lmin, rmin);
  262.     int last_pixel = max(lmax, rmax);
  263.     CONTRIB *p;
  264.  
  265.     if (last_pixel > last_index)
  266.         last_index = last_pixel;
  267.     contrib[i].first_pixel = (first_pixel % modulus) * stride;
  268.     contrib[i].n = last_pixel - first_pixel + 1;
  269.     contrib[i].index = i * npixels;
  270.     p = items + contrib[i].index;
  271.     for (j = 0; j < npixels; ++j)
  272.         p[j].weight = 0;
  273.     if (squeeze) {
  274.         for (j = left; j <= right; ++j) {
  275.         double weight =
  276.         fproc((center - j) / fscale) / fscale;
  277.         int n = clamp_pixel(j);
  278.         int k = n - first_pixel;
  279.  
  280.         p[k].weight +=
  281.             (PixelWeight) (weight * scaled_factor);
  282.         }
  283.     } else {
  284.         for (j = left; j <= right; ++j) {
  285.         double weight = fproc(center - j);
  286.         int n = clamp_pixel(j);
  287.         int k = n - first_pixel;
  288.  
  289.         p[k].weight +=
  290.             (PixelWeight) (weight * scaled_factor);
  291.         }
  292.     }
  293.     }
  294.     return last_index;
  295. }
  296.  
  297.  
  298. /* Apply filter to zoom horizontally from src to tmp. */
  299. private void
  300. zoom_x(PixelTmp * tmp, const void /*PixelIn */ *src, int sizeofPixelIn,
  301.        int tmp_width, int WidthIn, int Colors, const CLIST * contrib,
  302.        const CONTRIB * items)
  303. {
  304.     int c, i;
  305. #ifdef NEED_FRACTION_BITS
  306.     const int fraction_bits =
  307.     (sizeofPixelIn - sizeof(PixelTmp)) * 8 + num_weight_bits;
  308. #endif
  309.  
  310.     for (c = 0; c < Colors; ++c) {
  311.     PixelTmp *tp = tmp + c;
  312.     const CLIST *clp = contrib;
  313.  
  314.     if_debug1('W', "[W]zoom_x color %d:", c);
  315.  
  316. #define zoom_x_loop(PixelIn, PixelIn2)\
  317.         const PixelIn *raster = (const PixelIn *)src + c;\
  318.         for ( i = 0; i < tmp_width; tp += Colors, ++clp, ++i )\
  319.           {    AccumTmp weight = 0;\
  320.             { int j = clp->n;\
  321.               const PixelIn *pp = raster + clp->first_pixel;\
  322.               const CONTRIB *cp = items + clp->index;\
  323.               switch ( Colors )\
  324.               {\
  325.               case 1:\
  326.                 for ( ; j > 0; pp += 1, ++cp, --j )\
  327.                   weight += *pp * cp->weight;\
  328.                 break;\
  329.               case 3:\
  330.                 for ( ; j > 0; pp += 3, ++cp, --j )\
  331.                   weight += *pp * cp->weight;\
  332.                 break;\
  333.               default:\
  334.                 for ( ; j > 0; pp += Colors, ++cp, --j )\
  335.                   weight += *pp * cp->weight;\
  336.               }\
  337.             }\
  338.             { PixelIn2 pixel = unscale_AccumTmp(weight, fraction_bits);\
  339.               if_debug1('W', " %ld", (long)pixel);\
  340.               *tp =\
  341.                 (PixelTmp)CLAMP(pixel, minPixelTmp, maxPixelTmp);\
  342.             }\
  343.           }
  344.  
  345.     if (sizeofPixelIn == 1) {
  346.         zoom_x_loop(byte, int)
  347.     } else {        /* sizeofPixelIn == 2 */
  348. #if arch_ints_are_short
  349.         zoom_x_loop(bits16, long)
  350. #else
  351.         zoom_x_loop(bits16, int)
  352. #endif
  353.     }
  354.     if_debug0('W', "\n");
  355.     }
  356. }
  357.  
  358.  
  359. /*
  360.  * Apply filter to zoom vertically from tmp to dst.
  361.  * This is simpler because we can treat all columns identically
  362.  * without regard to the number of samples per pixel.
  363.  */
  364. private void
  365. zoom_y(void /*PixelOut */ *dst, int sizeofPixelOut, uint MaxValueOut,
  366.        const PixelTmp * tmp, int WidthOut, int tmp_width,
  367.        int Colors, const CLIST * contrib, const CONTRIB * items)
  368. {
  369.     int kn = WidthOut * Colors;
  370.     int cn = contrib->n;
  371.     int first_pixel = contrib->first_pixel;
  372.     const CONTRIB *cbp = items + contrib->index;
  373.     int kc;
  374.     PixelTmp2 max_weight = MaxValueOut;
  375. #ifdef NEED_FRACTION_BITS
  376.     const int fraction_bits =
  377.     (sizeof(PixelTmp) - sizeofPixelOut) * 8 + num_weight_bits;
  378. #endif
  379.  
  380.     if_debug0('W', "[W]zoom_y: ");
  381.  
  382. #define zoom_y_loop(PixelOut)\
  383.     for ( kc = 0; kc < kn; ++kc ) {\
  384.         AccumTmp weight = 0;\
  385.         { const PixelTmp *pp = &tmp[kc + first_pixel];\
  386.           int j = cn;\
  387.           const CONTRIB *cp = cbp;\
  388.           for ( ; j > 0; pp += kn, ++cp, --j )\
  389.             weight += *pp * cp->weight;\
  390.         }\
  391.         { PixelTmp2 pixel = unscale_AccumTmp(weight, fraction_bits);\
  392.           if_debug1('W', " %d", pixel);\
  393.           ((PixelOut *)dst)[kc] =\
  394.             (PixelOut)CLAMP(pixel, 0, max_weight);\
  395.         }\
  396.     }
  397.  
  398.     if (sizeofPixelOut == 1) {
  399.     zoom_y_loop(byte)
  400.     } else {            /* sizeofPixelOut == 2 */
  401.     zoom_y_loop(bits16)
  402.     }
  403.     if_debug0('W', "\n");
  404. }
  405.  
  406. /* ------ Stream implementation ------ */
  407.  
  408. #define tmp_width params.WidthOut
  409. #define tmp_height params.HeightIn
  410.  
  411. /* Forward references */
  412. private void s_IScale_release(P1(stream_state * st));
  413.  
  414. /* Calculate the weights for an output row. */
  415. private void
  416. calculate_dst_contrib(stream_IScale_state * ss, int y)
  417. {
  418.     uint row_size = ss->params.WidthOut * ss->params.Colors;
  419.     int last_index =
  420.     calculate_contrib(&ss->dst_next_list, ss->dst_items, ss->yscale,
  421.               y, 1, ss->params.HeightIn, MAX_ISCALE_SUPPORT, row_size,
  422.               (double)ss->params.MaxValueOut / (fixedScaleFactor * unitPixelTmp) );
  423.     int first_index_mod = ss->dst_next_list.first_pixel / row_size;
  424.  
  425.     ss->dst_last_index = last_index;
  426.     last_index %= MAX_ISCALE_SUPPORT;
  427.     if (last_index < first_index_mod) {        /* Shuffle the indices to account for wraparound. */
  428.     CONTRIB shuffle[MAX_ISCALE_SUPPORT];
  429.     int i;
  430.  
  431.     for (i = 0; i < MAX_ISCALE_SUPPORT; ++i)
  432.         shuffle[i].weight =
  433.         (i <= last_index ?
  434.          ss->dst_items[i + MAX_ISCALE_SUPPORT - first_index_mod].weight :
  435.          i >= first_index_mod ?
  436.          ss->dst_items[i - first_index_mod].weight :
  437.          0);
  438.     memcpy(ss->dst_items, shuffle, MAX_ISCALE_SUPPORT * sizeof(CONTRIB));
  439.     ss->dst_next_list.n = MAX_ISCALE_SUPPORT;
  440.     ss->dst_next_list.first_pixel = 0;
  441.     }
  442. #ifdef DEBUG
  443.     if (gs_debug_c('w')) {
  444.     dprintf1("[w]calc dest contrib for y = %d\n", y);
  445.     }
  446. #endif
  447. }
  448.  
  449. /* Set default parameter values (actually, just clear pointers). */
  450. private void
  451. s_IScale_set_defaults(stream_state * st)
  452. {
  453.     stream_IScale_state *const ss = (stream_IScale_state *) st;
  454.  
  455.     ss->src = 0;
  456.     ss->dst = 0;
  457.     ss->tmp = 0;
  458.     ss->contrib = 0;
  459.     ss->items = 0;
  460. }
  461.  
  462. /* Initialize the filter. */
  463. private int
  464. s_IScale_init(stream_state * st)
  465. {
  466.     stream_IScale_state *const ss = (stream_IScale_state *) st;
  467.     gs_memory_t *mem = ss->memory;
  468.  
  469.     ss->sizeofPixelIn = ss->params.BitsPerComponentIn / 8;
  470.     ss->sizeofPixelOut = ss->params.BitsPerComponentOut / 8;
  471.     ss->xscale = (double)ss->params.WidthOut / (double)ss->params.WidthIn;
  472.     ss->yscale = (double)ss->params.HeightOut / (double)ss->params.HeightIn;
  473.  
  474.     ss->src_y = 0;
  475.     ss->src_size = ss->params.WidthIn * ss->sizeofPixelIn * ss->params.Colors;
  476.     ss->src_offset = 0;
  477.     ss->dst_y = 0;
  478.     ss->dst_size = ss->params.WidthOut * ss->sizeofPixelOut * ss->params.Colors;
  479.     ss->dst_offset = 0;
  480.  
  481.     /* create intermediate image to hold horizontal zoom */
  482.     ss->tmp = (PixelTmp *) gs_alloc_byte_array(mem,
  483.                        min(ss->tmp_height, MAX_ISCALE_SUPPORT),
  484.                   ss->tmp_width * ss->params.Colors * sizeof(PixelTmp),
  485.                            "image_scale tmp");
  486.     ss->contrib = (CLIST *) gs_alloc_byte_array(mem,
  487.                        max(ss->params.WidthOut, ss->params.HeightOut),
  488.                       sizeof(CLIST), "image_scale contrib");
  489.     ss->items = (CONTRIB *) gs_alloc_byte_array(mem,
  490.                   contrib_pixels(ss->xscale) * ss->params.WidthOut,
  491.                  sizeof(CONTRIB), "image_scale contrib[*]");
  492.     /* Allocate buffers for 1 row of source and destination. */
  493.     ss->dst = gs_alloc_byte_array(mem, ss->params.WidthOut * ss->params.Colors,
  494.                   ss->sizeofPixelOut, "image_scale dst");
  495.     ss->src = gs_alloc_byte_array(mem, ss->params.WidthIn * ss->params.Colors,
  496.                   ss->sizeofPixelIn, "image_scale src");
  497.     if (ss->tmp == 0 || ss->contrib == 0 || ss->items == 0 ||
  498.     ss->dst == 0 || ss->src == 0
  499.     ) {
  500.     s_IScale_release(st);
  501.     return ERRC;
  502. /****** WRONG ******/
  503.     }
  504.     /* Pre-calculate filter contributions for a row. */
  505.     calculate_contrib(ss->contrib, ss->items, ss->xscale,
  506.               0, ss->params.WidthOut, ss->params.WidthIn, ss->params.WidthIn,
  507.               ss->params.Colors, (double)unitPixelTmp * fixedScaleFactor / ss->params.MaxValueIn);
  508.  
  509.     /* Prepare the weights for the first output row. */
  510.     calculate_dst_contrib(ss, 0);
  511.  
  512.     return 0;
  513.  
  514. }
  515.  
  516. /* Process a buffer.  Note that this handles Encode and Decode identically. */
  517. private int
  518. s_IScale_process(stream_state * st, stream_cursor_read * pr,
  519.          stream_cursor_write * pw, bool last)
  520. {
  521.     stream_IScale_state *const ss = (stream_IScale_state *) st;
  522.  
  523.     /* Check whether we need to deliver any output. */
  524.  
  525.   top:while (ss->src_y > ss->dst_last_index) {    /* We have enough horizontally scaled temporary rows */
  526.     /* to generate a vertically scaled output row. */
  527.     uint wleft = pw->limit - pw->ptr;
  528.  
  529.     if (ss->dst_y == ss->params.HeightOut)
  530.         return EOFC;
  531.     if (wleft == 0)
  532.         return 1;
  533.     if (ss->dst_offset == 0) {
  534.         byte *row;
  535.  
  536.         if (wleft >= ss->dst_size) {    /* We can scale the row directly into the output. */
  537.         row = pw->ptr + 1;
  538.         pw->ptr += ss->dst_size;
  539.         } else {        /* We'll have to buffer the row. */
  540.         row = ss->dst;
  541.         }
  542.         /* Apply filter to zoom vertically from tmp to dst. */
  543.         zoom_y(row, ss->sizeofPixelOut, ss->params.MaxValueOut, ss->tmp,
  544.            ss->params.WidthOut, ss->tmp_width, ss->params.Colors,
  545.            &ss->dst_next_list, ss->dst_items);
  546.         /* Idiotic C coercion rules allow T* and void* to be */
  547.         /* inter-assigned freely, but not compared! */
  548.         if ((void *)row != ss->dst)        /* no buffering */
  549.         goto adv;
  550.     } {            /* We're delivering a buffered output row. */
  551.         uint wcount = ss->dst_size - ss->dst_offset;
  552.         uint ncopy = min(wleft, wcount);
  553.  
  554.         memcpy(pw->ptr + 1, (byte *) ss->dst + ss->dst_offset, ncopy);
  555.         pw->ptr += ncopy;
  556.         ss->dst_offset += ncopy;
  557.         if (ncopy != wcount)
  558.         return 1;
  559.         ss->dst_offset = 0;
  560.     }
  561.     /* Advance to the next output row. */
  562.       adv:++(ss->dst_y);
  563.     if (ss->dst_y != ss->params.HeightOut)
  564.         calculate_dst_contrib(ss, ss->dst_y);
  565.     }
  566.  
  567.     /* Read input data and scale horizontally into tmp. */
  568.  
  569.     {
  570.     uint rleft = pr->limit - pr->ptr;
  571.     uint rcount = ss->src_size - ss->src_offset;
  572.  
  573.     if (rleft == 0)
  574.         return 0;        /* need more input */
  575. #ifdef DEBUG
  576.     assert(ss->src_y < ss->params.HeightIn);
  577. #endif
  578.     if (rleft >= rcount) {    /* We're going to fill up a row. */
  579.         const byte *row;
  580.  
  581.         if (ss->src_offset == 0) {    /* We have a complete row.  Read the data */
  582.         /* directly from the input. */
  583.         row = pr->ptr + 1;
  584.         } else {        /* We're buffering a row in src. */
  585.         row = ss->src;
  586.         memcpy((byte *) ss->src + ss->src_offset, pr->ptr + 1,
  587.                rcount);
  588.         ss->src_offset = 0;
  589.         }
  590.         /* Apply filter to zoom horizontally from src to tmp. */
  591.         if_debug2('w', "[w]zoom_x y = %d to tmp row %d\n",
  592.               ss->src_y, (ss->src_y % MAX_ISCALE_SUPPORT));
  593.         zoom_x(ss->tmp + (ss->src_y % MAX_ISCALE_SUPPORT) *
  594.            ss->tmp_width * ss->params.Colors, row,
  595.            ss->sizeofPixelIn, ss->tmp_width, ss->params.WidthIn,
  596.            ss->params.Colors, ss->contrib, ss->items);
  597.         pr->ptr += rcount;
  598.         ++(ss->src_y);
  599.         goto top;
  600.     } else {        /* We don't have a complete row.  Copy data to src buffer. */
  601.         memcpy((byte *) ss->src + ss->src_offset, pr->ptr + 1, rleft);
  602.         ss->src_offset += rleft;
  603.         pr->ptr += rleft;
  604.         return 0;
  605.     }
  606.     }
  607. }
  608.  
  609. /* Release the filter's storage. */
  610. private void
  611. s_IScale_release(stream_state * st)
  612. {
  613.     stream_IScale_state *const ss = (stream_IScale_state *) st;
  614.     gs_memory_t *mem = ss->memory;
  615.  
  616.     gs_free_object(mem, (void *)ss->src, "image_scale src");    /* no longer const */
  617.     ss->src = 0;
  618.     gs_free_object(mem, ss->dst, "image_scale dst");
  619.     ss->dst = 0;
  620.     gs_free_object(mem, ss->items, "image_scale contrib[*]");
  621.     ss->items = 0;
  622.     gs_free_object(mem, ss->contrib, "image_scale contrib");
  623.     ss->contrib = 0;
  624.     gs_free_object(mem, ss->tmp, "image_scale tmp");
  625.     ss->tmp = 0;
  626. }
  627.  
  628. /* Stream template */
  629. const stream_template s_IScale_template = {
  630.     &st_IScale_state, s_IScale_init, s_IScale_process, 1, 1,
  631.     s_IScale_release, s_IScale_set_defaults
  632. };
  633.